package com.hxzy.controller.cellinfo;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.util.BeanValidatorUtil;
import com.hxzy.common.BaseServlet;
import com.hxzy.common.ResultAjax;
import com.hxzy.entity.Cellinfo;
import com.hxzy.service.cellinfoService;
import com.hxzy.service.impl.cellinfoSerxiceIpml;
import com.hxzy.util.SnowFlakeUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @Description:
 * @Author: YangYong
 * @Date: 2022/3/8 15:55
 */
@WebServlet(urlPatterns = "/cellinfo/add")
public class cellinfo_add extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        try{
            Cellinfo cellinfo=super.CollectiongParameterToJSON(req, Cellinfo.class);
           SnowFlakeUtil snowFlakeUtil=new SnowFlakeUtil(0, 0);
            Long l = snowFlakeUtil.nextId();
            cellinfo.setId(l.toString());
            Set<String> validator= BeanValidatorUtil.validator(Cellinfo.class,cellinfo);
            if(validator.isEmpty()){
                //修改明文密码为加密的密码(长度验证)
                //调有和新增操作

                cellinfoService cellinfoService=new cellinfoSerxiceIpml();
                int count = cellinfoService.insert(cellinfo);

                ResultAjax resultAjax = null;
                if(count>0){
                    resultAjax =ResultAjax.succss_data(cellinfo);
                }else {
                    resultAjax=ResultAjax.error("错误");
                }
                super.render(resp, JSONObject.toJSONString(resultAjax));
            }else{
                String message = validator.iterator().next();

                ResultAjax resultAjax = ResultAjax.error(101, message);

                super.render(resp, JSONObject.toJSONString(resultAjax));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
